/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.City;
import cc.altius.model.MasterZone;
import cc.altius.model.State;
import cc.altius.service.CityService;
import cc.altius.service.ZoneService;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author santosh
 */
@Controller
public class MasterZoneController {

    @Autowired
    private ZoneService zoneService;
    @Autowired
    private CityService cityService;

    @ModelAttribute("cityList")
    public List<City> populateCityList() {

        return this.zoneService.getCityList();

    }

//    @ModelAttribute("masterZoneList")
//    public List<MasterZone> populateMasterZoneList() {
//
//        return this.zoneService.getMasterZoneList();
//    }
    @ModelAttribute("stateList")
    public List<State> populateStateList() {
        List<State> stateList = this.cityService.getStateList();
        return stateList;
    }

    @RequestMapping(value = "/masterzone/addMasterZone.htm", method = RequestMethod.GET)
    public String showaddMasterZone(ModelMap modelMap) {
        MasterZone masterZone = new MasterZone();
        modelMap.addAttribute("masterZone", masterZone);
        return "masterzone/addMasterZone";
    }

    @RequestMapping(value = "/masterzone/addMasterZone.htm", method = RequestMethod.POST)
    public String addMasterZone(@ModelAttribute("masterZone") MasterZone masterZone,Errors errors, HttpServletRequest request,
            SessionStatus sessionStatus) {
        if (this.zoneService.existMasterZoneByMasterZoneCode(masterZone.getMasterZoneCode())) {
            errors.rejectValue("masterZoneCode", "msg.duplicatemasterZoneCode");
            //errors.rejectValue("zoneCode", "msg.duplicateZoneCode");
            return "/masterzone/addMasterZone";
        }
        
        int masterZoneId = this.zoneService.addMasterZone(masterZone);
        if (masterZoneId == 0) {
            errors.rejectValue("masterZoneCode", "msg.masterZoneCodeError");
            return "/masterzone/addMasterZone";
        } else {
            sessionStatus.setComplete();
            return "redirect:../masterzone/listMasterZone.htm?msg=add master zone successfully..";
        }
        
    }

    @RequestMapping(value = "/masterzone/listMasterZone.htm", method = RequestMethod.GET)
    public String showListMasterZone(ModelMap modelMap) {
        List<Map<String, Object>> filterMasterZone = this.zoneService.getFilterMasterZone(0, 0);
        modelMap.addAttribute("filterMasterZone", filterMasterZone);
        modelMap.addAttribute("cityId", 0);
        modelMap.addAttribute("stateId", 0);
        return "masterzone/listMasterZone";
    }

    @RequestMapping(value = "/masterzone/listMasterZone.htm", method = RequestMethod.POST)
    public String filterOnMasterZone(@RequestParam(value = "stateId", required = true, defaultValue = "0") int stateId,
            @RequestParam(value = "cityId", required = true, defaultValue = "0") int cityId, ModelMap modelMap) {

        List<Map<String, Object>> filterMasterZone = this.zoneService.getFilterMasterZone(stateId, cityId);
        modelMap.addAttribute("filterMasterZone", filterMasterZone);
        modelMap.addAttribute("cityId", cityId);
        modelMap.addAttribute("stateId", stateId);
        return "/masterzone/listMasterZone";
    }

    @RequestMapping(value = "/masterzone/editMasterZone.htm", method = RequestMethod.GET)
    public String showEditMasterZone(@RequestParam("masterZoneId") String masterZoneId, ModelMap modelMap) {
        MasterZone masterZone = this.zoneService.getMasterZoneById(Integer.parseInt(masterZoneId));
        System.out.println("master id=" + masterZone.getMasterZoneId());
        modelMap.addAttribute("masterZone", masterZone);
        return "masterzone/editMasterZone";
    }

    @RequestMapping(value = "/masterzone/editMasterZone.htm", method = RequestMethod.POST)
    public String showUpdateMasterZone(@ModelAttribute("masterZone") MasterZone masterZone) {

        System.out.println("in update:post");
        System.out.println("master id=" + masterZone.getMasterZoneId());
        int masterZoneId = this.zoneService.updateMasterZone(masterZone);
        if (masterZoneId != 0) {
            return "redirect:../masterzone/listMasterZone.htm?msg=update success..";
        } else {
            return "redirect:../masterzone/editMasterZone.htm?msg=update failed..";
        }

    }
}
